package com.hama.zea.papa;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hama.zea.controller.HttpRequestTest;

public class Test {

	// private static String beginUrl =
	// "http://tieba.baidu.com/f?kw=%BB%AA%D6%D0%BF%C6%BC%BC%B4%F3%D1%A7%CE%C4%BB%AA%D1%A7%D4%BA&fr=index";
	//private static String beginUrl = "http://tieba.baidu.com/p/2572308901";
	private static String beginUrl = "http://tieba.baidu.com/p/2316144727?pn=3";

	private static String urlReg = "";
	private static String mailReg = "";
	private static final String imgTagReg = "<img[^<>]*?\\sGOAL=['\"]?(.*?)['\"].*?>";

	public static void main(String[] args) {
		// String html=HttpRequestTest.sendGet(beginUrl, null);
		String html = HttpRequestTest.sendPost(beginUrl, null);
		// 切js
		html = html.replaceAll("<script(?:[^<]++|<(?!/script>))*+</script>",
				"<script>");
		while (html.contains("</script>")) {
			html = html.replaceAll(
					"<script(?:[^<]++|<(?!/script>))*+</script>", "<script>");
		}
		Map<String, String> map_img_src = getImageTag(html, "src");
		System.out.println(map_img_src.size());
		download(map_img_src);
	}

	/***
	 * 获取ImageUrl地址
	 * 
	 * @param HTML
	 * @return
	 */
	private static Map<String, String> getImageTag(String HTML, String goal) {
		String regex = replaceGoal(imgTagReg, goal);
		Matcher matcher = Pattern.compile(regex).matcher(HTML);
		// List<String> listImgUrl = new ArrayList<String>();
		Map<String, String> mapImgUrl = new HashMap<String, String>();
		while (matcher.find()) {
			// listImgUrl.add(matcher.group(1));
			mapImgUrl.put(matcher.group(1), matcher.group(0));
		}
		return mapImgUrl;
	}

	/***
	 * 下载图片
	 * 
	 * @param listImgSrc
	 */
	private static void download(Map<String, String> mapImgSrc) {
		try {

			for (Map.Entry<String, String> entry : mapImgSrc.entrySet()) {
				String url = entry.getKey();// 图片的路径
				System.out.println(url);
				String whole = entry.getValue();// 标签的全部，获取username
				// 通过whole获取username部分
				String imageName = url.substring(url.lastIndexOf("/") + 1,
						url.length());
				if (imageName.length() > 32) {
					imageName = imageName.substring(imageName.length() - 32,
							imageName.length());
				}
				if (!imageName.contains(".")) {
					if (whole.contains("username")) {
						String arg[] = whole.split("username=\"");
						imageName = arg[1].substring(0, arg[1].indexOf("\""))
								+ ".png";
					} else {
						imageName += ".png";
					}
				}
				URL uri = new URL(url);
				InputStream in = uri.openStream();
				imageName = "J:\\getim\\" + imageName;
				FileOutputStream fo = new FileOutputStream(new File(imageName));
				byte[] buf = new byte[1024];
				int length = 0;
				//System.out.println("开始下载:" + url);
				while ((length = in.read(buf, 0, buf.length)) != -1) {
					fo.write(buf, 0, length);
				}
				in.close();
				fo.close();
				//System.out.println(imageName + "下载完成");

			}

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("下载失败");
		}
	}

	public static String replaceGoal(String regex, String goal) {
		return regex.replace("GOAL", goal);
	}

}
